//package com.utils.http;
//
//import java.io.IOException;
//import java.io.OutputStream;
//import java.util.Arrays;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import com.alibaba.fastjson.JSONObject;
//import com.kyhd.adexchange.controller.model.EsModel;
//import com.kyhd.adexchange.controller.model.EsModel.Aggregations;
//import com.kyhd.adexchange.controller.model.EsModel.Responses;
//import com.kyhd.adexchange.controller.model.EsModel.Aggregations._2;
//import com.kyhd.adexchange.controller.model.EsModel.Aggregations._2.Buckets;
//import com.kyhd.adexchange.util.HttpClientPost;
//
//import io.swagger.annotations.ApiOperation;
//
//public class SpringReturnHTML {
//
//		//发送数据，发送今天(统计展现数)
//		@ApiOperation(value = "", notes = "")
//		@RequestMapping(method = { RequestMethod.POST }, path = "/data")
//		public @ResponseBody ResponseEntity<String> data(HttpServletRequest request, HttpServletResponse response) {
//			log.info("data request");
//			HttpHeaders headers = new HttpHeaders();
//			headers.put(HttpHeaders.CONTENT_TYPE, Arrays.asList("application/json;charset=UTF-8"));
//			
//			//调起kibana的Dashboard的查询功能
//			String apiUrl = "http://39.106.195.234:5602/elasticsearch/_msearch";
//			HttpClientPost.createDefaultHttpClient();
//			Map<String, String> header = new HashMap<String, String>();
////			header.put("Accept", "application/json, text/plain, */*");
//			header.put("Accept-Encoding", "gzip, deflate");
//			header.put("Accept-Language", "zh-CN,zh;q=0.9");
//			header.put("content-type", "application/x-ndjson");
//			header.put("kbn-name", "kibana");
//			header.put("kbn-version", "6.2.4");
//			header.put("kbn-xpack-sig", "a4266346372fb760b48cfb517806a17c");
//			header.put("User-Agen", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36");
//			header.put("Cookie",cookie2);
//			String json = "{\"index\":[\"pixels-*\"],\"ignore_unavailable\":true,\"preference\":1548399506034}\r\n" + 
//					"{\"size\":0,\"_source\":{\"excludes\":[]},\"aggs\":{\"2\":{\"terms\":{\"field\":\"creative_id.keyword\",\"size\":20,\"order\":{\"1\":\"desc\"}},\"aggs\":{\"1\":{\"cardinality\":{\"field\":\"bid_id.keyword\"}}}}},\"version\":true,\"stored_fields\":[\"*\"],\"script_fields\":{},\"docvalue_fields\":[\"@timestamp\"],\"query\":{\"bool\":{\"must\":[{\"match_all\":{}},{\"match_all\":{}},{\"range\":{\"@timestamp\":{\"gte\":1547799633862,\"lte\":1548404433862,\"format\":\"epoch_millis\"}}}],\"filter\":[],\"should\":[],\"must_not\":[]}},\"highlight\":{\"pre_tags\":[\"@kibana-highlighted-field@\"],\"post_tags\":[\"@/kibana-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647}}";
//			HttpClientPost.setHeader(header);
//			String logmsg = HttpClientPost.sendHttpPostBody(apiUrl, json);
//			
//			EsModel datas = JSONObject.parseObject(logmsg,EsModel.class);
//			List<Responses> responses = datas.getResponses();
//			
//			StringBuilder responseHtml = new StringBuilder("<html><head>展现数据:</head><body>\r\n");
//			responseHtml.append("<table border=\"1\">");
//			responseHtml.append("  <tr><th>创意id</th><th>展现数</th></tr>\r\n");
//			
//			Long total = 0l;
//			int price = 15;
//			Long consume = 0l;
//			
//			if(responses!=null&&responses.size()>0&&responses.get(0)!=null) {
//				Aggregations aggregations = responses.get(0).getAggregations();
//				if(aggregations!=null) {
//					_2 two = aggregations.get_2();
//					if(two!=null) {
//						List<Buckets> buckets = two.getBuckets();
//						if(buckets!=null&&buckets.size()>0) {
//							for (Buckets buckets2 : buckets) {
//								responseHtml.append("  <tr><td>"+buckets2.getKey()+"</td><td>"+buckets2.getDoc_count()+"</td></tr>\r\n");
//								total =+ Long.valueOf(buckets2.getDoc_count()+"");
//							}
//						}
//					}
//				}
//			}
//			
//			responseHtml.append("<table border=\"0\"><tr><td>总计:</td><td>"+total+"</td></tr>\r\n");
//			responseHtml.append("<tr><td>单价:</td><td>"+price+"</td></tr>");
//			responseHtml.append("<tr><td>消费:</td><td>"+consume+"</td></tr></table>");
//			responseHtml.append("</body></html>");
//			
//			OutputStream p = null;
//			try {
//				p = response.getOutputStream();
//				p.write(responseHtml.toString().getBytes());
//			} catch (IOException e) {
//				e.printStackTrace();
//			} finally {
//				if (p != null) {
//					try {
//						p.close();
//					} catch (IOException e) {
//						e.printStackTrace();
//					}
//				}
//			}
//			log.info("data response:{}",response);
//			return new ResponseEntity<String>(headers, HttpStatus.OK);
//		}
//		
//}
